#include "Wakeup.h"
#include "RingBuffer.h"

typedef short RINGBUFFER[BUFLEN];

static int m_id;
static RINGBUFFER * m_Buffer = 0;

void RingBufferInit(void)
{
	m_Buffer = (RINGBUFFER*)MALLOC(sizeof(RINGBUFFER)*HDRCNT);
	RingBufferClear();
}

void RingBufferRelease(void)
{
	if(m_Buffer)
	{
		FREE(m_Buffer);
		m_Buffer = 0;
	}
}

void RingBufferClear(void)
{
	m_id = 0;
	memset(m_Buffer, 0, sizeof(RINGBUFFER)*HDRCNT);
}

void RingBufferSetWaveData(short* data, int len)
{
	memcpy(m_Buffer[m_id], data, sizeof(short)*BUFLEN);

	m_id++;
	m_id %= HDRCNT;
}

void RingBufferGetWaveData(short * WavInput)
{
	int i;
	for(i=0; i<HDRCNT; i++)
	{
		int id = m_id+i;
		memcpy(&WavInput[i*BUFLEN], m_Buffer[id%HDRCNT], sizeof(short)*BUFLEN);
	}
}
